useLoadingState
The useLoadingState hook provides access to the current loading state of objects in the viewer. It is part of the @promaton/scan-viewer package and offers a comprehensive interface for managing and monitoring the loading process of viewer objects.
Interface: LoadingState
The LoadingState interface exposes various properties and methods to handle loading, processing, and error states for viewer objects.
Properties and Methods
addLoadingError(objectId: string, message: string): void
Adds a loading error for a specific ViewerObject by its ID, along with an error message.
addObjectToPostProcessingQueue(id: string): void
Adds an object to the post-processing queue, indicating that it will undergo post-processing.
addProcessingTask(task: ProcessingTask): () => void
Adds a processing task to the queue and returns a function to remove the task.
clearLoadingProgress(): void
Resets the loading progress.
completeLoadingProgress(): void
Marks the loading progress as complete.
firstLoadComplete: boolean
Indicates whether the initial load of a scene (excluding hidden objects) has been completed.
lastActive: string
Tracks the last file that made loading progress.
loadedViewerObjects: string[]
Lists the IDs of ViewerObjects that have been loaded, parsed, and initialized in the viewer at least once.
loadingErrors: string[]
Contains the IDs of files that encountered errors during loading.
loadingViewerObjects: string[]
Lists the IDs of ViewerObjects that are currently in the process of loading.
notifyViewerObjectLoaded(id: string): void
Notifies that a ViewerObject has been successfully loaded.
notifyViewerObjectLoading(id: string): void
Notifies that a ViewerObject is currently loading.
notifyViewerObjectRemoved(id: string): void
Unmarks an object as loaded or loading.
objectLoadingProgress: ObjectLoadingStates
A map containing the loading progress of all visible objects.
objectPostProcessingQueue: string[]
Lists objects that are undergoing post-processing after the initial load.
processingTasks: ProcessingTask[]
Lists external processing tasks to be displayed in the loading indicator.
removeObjectFromPostProcessingQueue(id: string): void
Removes an object from the post-processing queue.
totalProgress: number
Represents the total loading progress as a percentage (0-100).
updateLoadingProgress(id: string, loaded?: number, total?: number): void
Updates the loading progress of a specific item. The total parameter typically represents the total loading effort (e.g., in bytes).
Example Usage
import { useLoadingState } from "@promaton/scan-viewer";
function ViewerComponent() {
const loadingState = useLoadingState();
// Example: Notify that an object is loading
loadingState.notifyViewerObjectLoading("object1");
// Example: Update loading progress
loadingState.updateLoadingProgress("object1", 50, 100);
// Example: Mark loading as complete
loadingState.completeLoadingProgress();
return <div>Loading progress: {loadingState.totalProgress}%</div>;
}
The useLoadingState hook is a powerful tool for managing the loading lifecycle of viewer objects, providing fine-grained control over loading, processing, and error handling.